package com.example.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.Dto.PlaceOrderDto;
import com.example.demo.Dto.UpdateOrderDto;
import com.example.demo.Service.OrderService;
import com.example.demo.po.Order;
import com.example.demo.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/orders")
public class OrdersController {
    @Autowired
    private OrderService ordersService;
    /**
     * 创建订单
     * @return
     */
    @PostMapping
    public   ResponseResult addOrders(@RequestBody PlaceOrderDto placeOrderDto){
        String s = ordersService.addOrders(placeOrderDto);
        return new ResponseResult().ok("200","订单添加成功", s);
    }
    /**
     *  修改订单信息
     * @return
     */
    @PutMapping
    public ResponseResult UpdateOrders(@RequestBody UpdateOrderDto updateOrderDto){
        ordersService.updateOrder(updateOrderDto);
        return new ResponseResult().ok("200","获取订单信息成功");
    }
    /**
     * 订单ID查询订单信息
     * @return
     */
    @GetMapping("/{orderId}")
    public ResponseResult getOrderById(@PathVariable String orderId){
        return new ResponseResult().ok("200","获取订单信息成功",  ordersService.getOrderById(orderId));
    }
    /**
     * 订单ID删除订单信息
     * @return
     */
    @DeleteMapping("/{orderId}")
    public ResponseResult deleteOrderById(@PathVariable String orderId){
        if (ordersService.update(new UpdateWrapper<Order>().eq("order_id", orderId).set("status", -1))){
            return new ResponseResult().ok("200","删除订单信息成功");
        }else {
            throw new RuntimeException("删除订单信息失败");
        }
    }
}
